﻿//-----------------------------------------------------------------------
// <copyright file="HelperExtensions.cs" company="Microsoft 
// Corporation">
// Copyright (c) Microsoft Corporation.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Anil Dwarakanath</author>
// <email>anildwa@microsoft.com</email>
// <date>31-Dec-2010</date>
// <summary>Extension Methods for frequently used scenarios</summary>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Web.UI.WebControls;


namespace ContentAggregationWebPart
{

    #region Helper Method Class


    /// <summary>
    /// Static Class Contained Extension and Helper Methods
    /// </summary>
    public static class HelperExtensions
    {

        #region Extension Methods

        /// <summary>
        /// Deserializes a generic type and return string representating the object
        /// </summary>
        /// <param name="SerializedObject">String representing the object</param>
        public static object ToUserDefinedType<T>(this string SerializedObject)
        {
            StringReader reader = new StringReader(SerializedObject);

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(reader);


        }

        /// <summary>
        /// Deserializes a generic type and return string representating the object
        /// </summary>
        /// <param name="Object">Object to be deserialized</param>
        public static string ToString<T>(this T customSPField)
        {
            StringWriter writer = new StringWriter();

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(writer, customSPField);

            return writer.ToString();

        }


        /// <summary>
        /// Sorts a given collection
        /// </summary>
        /// <param name="items">Items Collection to be Sorted</param>
        public static void Sort(this ListItemCollection items)
        {
            IList<ListItem> itemList = new List<ListItem>();
            foreach (ListItem item in items)
                itemList.Add(item);

            IEnumerable<ListItem> itemEnum = from item in itemList orderby item.Text select item;

            items.Clear();
            items.AddRange(itemEnum.ToArray());
        }


        #endregion

        #region Static Methods

        

        /// <summary>
        /// Gets the Internal Name for a given field
        /// </summary>
        /// <param name="ViewFields">View Fields Collection</param>
        /// <param name="items">Display Name of the Field</param>
        public static string GetInternalName(List<string> ViewFields, string DisplayName)
        {
            foreach (string item in ViewFields)
            {
                if(DisplayName == ((ExtendedDisplayColumnType)item.ToUserDefinedType<ExtendedDisplayColumnType>()).Title)
                {

                    return ((ExtendedDisplayColumnType)item.ToUserDefinedType<ExtendedDisplayColumnType>()).Name;
                }
               
            }

            return "";

        }
        #endregion
    }

    #endregion

    #region Template Classes


    /// <summary>
    /// Template Class for representing fields on WebPart Controls
    /// </summary>
    [Serializable]
    public class BaseSPType
    {
        public string Guid { get; set; }
        public string Title { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
    }


    [Serializable]
    public class ExtendedDisplayColumnType
    {
        public string Title { get; set; }
        public string Name { get; set; }
        public bool IsHyperLink { get; set; }
        public string LinkUrl { get; set; }
        public bool IsVisible { get; set; }
        public bool IsClickToExpand { get; set; }
    }
    #endregion
}
